<?php

class HiveToMysql extends DataXActivity {

	public function __construct($activity) {
		parent::__construct($activity);
	}
	
	protected function prepareInputs() {
		// Because we are in hive action context, we load input physical resource as HIVE
		 
    	$physicalResource = $this->resourceService->getResourcePhysicalByType($this->output['resource']['res_id'], PhysicalType::Hive);
        if(!isset($physicalResource)) {
        	$this->setRuntimeInfo(array(
				'state'	=>	TaskState::Failed,
				'message'	=>	"[HiveToMysql/prepare]can't find physical as hive for resource[{$this->input['resource']['res_name']}]",
			));
            Yii::log("can't find physical as hive for resource[{$this->input['resource']['res_name']}]", CLogger::LEVEL_ERROR, WORKER_LOG);
            return false;
        }
        $this->input['resource']['physical'] = $physicalResource;
        return true;
	}
	
	protected function prepareOutputs() {
		if(false === $this->resourceService->editResourceAttributes($this->output['resource']['res_id'], array('job_id'=>$this->env['JOBID']))) {
			 $this->setRuntimeInfo(array(
				'state'	=>	TaskState::Failed,
				'message'	=>	"edit output resource [{$this->output['resource']['res_name']}] job_id failed",
			));
			return false;
		}
		// Create output physical resouce as Database
		 $outputPhysical = array(
			'physical_name'	=>	"{$this->output['resource']['res_name']}_mysql",
			'res_id'		=>	$this->output['resource']['res_id'],
			'storage'		=>	$this->output['resource']['res_name'],
	        'physical_type'	=>	PhysicalType::Database,
		 	'partition_format'=> 'Ymd/Hi',
	        'system_id'		=>	$this->output['system_id'],
	        'keep_days'		=>	5,
        );
        
        $physicalId = $this->resourceService->addResourcePhysical($outputPhysical);
	 	if(!$physicalId) {
	 		$this->setRuntimeInfo(array(
				'state'	=>	TaskState::Failed,
				'message'	=>	"[HiveToMysql/prepare]add output physical as database for resource[{$this->output['resource']['res_name']}] failed",
			));
            Yii::log("add output physical as database for resource[{$this->output['resource']['res_name']}] failed", CLogger::LEVEL_ERROR, WORKER_LOG);
            return false;
        }
        $outputPhysical['physical_id'] = $physicalId;
		$this->output['resource']['physical'] = $outputPhysical;
		return true;
	}
	
	protected function preparePrograms() {
		$programs = CJSON::decode($this->activity['programs']);    	
		$sourceSystem	= $this->resourceService->getStorageSystem($this->input['resource']['physical']['system_id']);
    	$destSystem		= $this->resourceService->getStorageSystem($this->output['resource']['physical']['system_id']);
    	
    	$dir = "hdfs://{$sourceSystem['host_name']}:{$sourceSystem['port']}{$sourceSystem['namespace']}/{$this->output['resource']['physical']['storage']}";
    	$programs[0] = $this->concreteProgram($programs[0], array(
    		'${ugi}'	=>	$sourceSystem['user_name'],
    		'${dir}'	=>	$dir,
    		'${columns}'	=>	'',
    		'${hostname}'	=>	$destSystem['host_name'],
    		'${username}'	=>	$destSystem['user_name'],
    		'${password}'	=>	$destSystem['password'],
    		'${dbname}'		=>	$destSystem['namespace'],
    		'${table}'		=>	$this->output['resource']['physical']['storage'],
    		)
    	);
    	return $programs;
	}
	
	
}
